package com.starmark.sys.org.provider.service.impl;


import com.starmark.core.framework.service.CommonIdToNameService;
import com.starmark.sys.org.api.dto.OrgElementDto;
import com.starmark.sys.org.api.service.IOrgUserApiService;
import com.starmark.sys.org.provider.constants.OrgApiConstant;
import com.starmark.sys.org.provider.dto.OrgTreeDto;
import com.starmark.sys.org.provider.service.ISysOrgCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * SysUser服务实现类
 *
 * @author starmark
 * @version 2016-08-27 22:39:42
 */
@Service("orgUserCommonService")
public class OrgUserCommonServiceImpl implements
        ISysOrgCommonService,CommonIdToNameService {

    @Autowired
    private IOrgUserApiService orgUserApiService;

    @Override
    public List<OrgTreeDto> getOrgTreeBySearchkey(String searchKey, int pageSize) {
        List<OrgElementDto> orgPersonDtos = orgUserApiService.getOrgTreeBySearchkey(searchKey, pageSize);
        return orgPersonDtos.stream().map(orgPersonDto -> new OrgTreeDto(orgPersonDto, OrgApiConstant.ORG_PERSON)).collect(Collectors.toList());
    }


    @Override
    public List<OrgTreeDto> getOrgTreeById(String id) {
        List<OrgElementDto> orgPersonDtos = orgUserApiService.getOrgTreeById(id);
        return orgPersonDtos.stream().map(orgPersonDto -> new OrgTreeDto(orgPersonDto, OrgApiConstant.ORG_PERSON)).collect(Collectors.toList());

    }


    @Override
    public boolean isSearch(int orgType) {
        return (orgType & OrgApiConstant.ORG_PERSON) == OrgApiConstant.ORG_PERSON;
    }


    @Override
    public boolean isTree(int orgType) {
        return (orgType & OrgApiConstant.ORG_PERSON) == OrgApiConstant.ORG_PERSON;
    }


    @Override
    public String getNameById(String id) {
        return orgUserApiService.getName(id);
    }
}
